using System;
using System.Net;
using Stocks;
using System.Collections.Specialized;
namespace StockDownloader
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class YahooAccessor
	{

		public YahooAccessor()
		{
			//
			// TODO: Add constructor logic here
			//
		}
        public static StockInstanceDataCollectionEx GetHistoricData(string stockSymbol, DateTime startDate, DateTime endDate)
        {
            string url = string.Format("http://ichart.finance.yahoo.com/table.csv?s={0}&d={1}&e={2}&f={3}&g=d&a={4}&b={5}&c={6}&ignore=.csv",
                stockSymbol,endDate.Month,endDate.Day,endDate.Year,
                startDate.Month,startDate.Day,startDate.Year
                );
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            WebResponse response ;
            
                response = request.GetResponse();
                System.IO.Stream stream = response.GetResponseStream();
                System.Text.Encoding ec = System.Text.Encoding.GetEncoding("utf-8");
                System.IO.StreamReader reader = new System.IO.StreamReader(stream, ec);
                //StringCollection sc = new StringCollection();
                StockInstanceDataCollectionEx sidCollection = new StockInstanceDataCollectionEx();
                string eol = Environment.NewLine;
                StockInstantData sid;
                sidCollection.Symbol = stockSymbol;
                while (!reader.EndOfStream)
                {

                    sid = new StockInstantData();

                    string[] sData = reader.ReadLine().Split(',');
                    if (sData[0] != "Date")
                    {
                        sid.Symbol = sData[0];
                        sid.Price = sData[0] != @"N/A" ? Convert.ToDecimal(sData[1]) : 0;
                        sid.DateTime = DateTime.Parse(sData[0]);

                        sid.OpenPrice = sData[1] != @"N/A" ? Convert.ToDecimal(sData[1]) : 0;
                        sid.HighPrice = sData[2] != @"N/A" ? Convert.ToDecimal(sData[2]) : 0;
                        sid.LowPrice = sData[3] != @"N/A" ? Convert.ToDecimal(sData[3]) : 0;
                        sid.DailyVolume = sData[5] != @"N/A" ? Convert.ToInt32(sData[5]) : 0;
                        sid.ClosePrice = sData[4] != @"N/A" ? Convert.ToDecimal(sData[4]) : 0;
                        sid.AdjClose = Convert.ToDecimal(sData[6]);
                        sid.Change = sid.ClosePrice - sid.OpenPrice;
                        sid.Symbol = stockSymbol;
                        sidCollection.Add(sid);
                    }
                    else
                        Console.WriteLine(string.Format("{0} {1} {2} {3} {4} {5} {6}", sData[0], sData[1], sData[2], sData[3], sData[4], sData[5], sData[6]));

                }
            
         
            return sidCollection;


        }
		public static StockInstantData GetFromSite(string stockSymbol)
		{
            
			string url = "http://finance.yahoo.com/d/quotes.csv?s=" + stockSymbol + "&f=sl1d1t1c1ohgv&e=.csv";
			HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
			string result="";
			WebResponse response = request.GetResponse();
			System.IO.Stream stream = response.GetResponseStream();
			System.Text.Encoding ec = System.Text.Encoding.GetEncoding("utf-8");
			System.IO.StreamReader reader = new System.IO.StreamReader(stream, ec);
			char [] chars = new Char[256];
			int count = reader.Read(chars, 0, 256);
			while(count > 0)
			{
				string str = new String(chars, 0, 256);
				result = result + str;
				count = reader.Read(chars, 0, 256);
			}
			response.Close();
			stream.Close();
			reader.Close();
			StockInstantData sid = new StockInstantData();
		
			string[] sData = result.Split(',');
			sid.Symbol = sData[0];
			sid.Price = sData[0] != @"N/A" ? Convert.ToDecimal(sData[1]) : 0;
            sid.DateTime = DateTime.Parse(sData[2].Replace("\"", "") + " " + sData[3].Replace("\"", ""));
			sid.Change = sData[4] != @"N/A" ? Convert.ToDecimal(sData[4]) : 0;
			sid.OpenPrice = sData[5] != @"N/A" ?  Convert.ToDecimal(sData[5]) : 0;
			sid.HighPrice = sData[6] != @"N/A" ? Convert.ToDecimal(sData[6]) : 0 ; 
			sid.LowPrice = sData[7] != @"N/A" ? Convert.ToDecimal(sData[7]) : 0; 
			sid.DailyVolume = sData[8] != @"N/A" ?  Convert.ToInt32(sData[8]) : 0;




			return sid;


		}

	}
}
